import os
import sys
# 当前目录下的大文件查找, 并且把结果输出在控制台上
# 默认扫描当前目录，阈值单位MB
target_dir = "."
threshold = 1024

def get_folder_size(folder_path):
    """计算文件夹大小（MB）"""
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder_path):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            try:
                total_size += os.path.getsize(filepath)
            except (OSError, PermissionError):
                continue
    return round(total_size / (1024 * 1024), 2)

def find_large_folders(directory, threshold_mb=100):
    """查找超过阈值大小的文件夹"""
    print(f"正在扫描目录: {directory}")
    print(f"查找超过 {threshold_mb} MB 的文件夹...")
    print("-" * 50)
    
    found_large = False
    for item in os.listdir(directory):
        item_path = os.path.join(directory, item)
        if os.path.isdir(item_path):
            try:
                size_mb = get_folder_size(item_path)
                if size_mb > threshold_mb:
                    found_large = True
                    print(f"文件夹: {item}")
                    print(f"路径: {item_path}")
                    print(f"大小: {size_mb} MB----{size_mb / 1024} GB")
                    print("-" * 50)
            except (PermissionError, OSError) as e:
                print(f"无法访问: {item_path} - {e}")
                continue
    
    if not found_large:
        print("未找到超过阈值大小的文件夹")
    print("扫描完成！")

if __name__ == "__main__":


    # 处理命令行参数
    if len(sys.argv) > 1:
        target_dir = sys.argv[1]
    if len(sys.argv) > 2:
        try:
            threshold = int(sys.argv[2])
        except ValueError:
            print("阈值必须是数字")
            sys.exit(1)
    
    if not os.path.exists(target_dir):
        print(f"目录不存在: {target_dir}")
        sys.exit(1)
    
    find_large_folders(target_dir, threshold)